package test.multiple_task;

import beautiful.butterfly.distributed_task_processing_engine.api.AbstractMultipleTaskDataItemCollectorAndExecuterAndExecuter;
import beautiful.butterfly.distributed_task_processing_engine.task_manage.task_data_items_processor.task_data_item_data_manager.TaskItemStringAndParameterString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * 单个任务处理实现
 */
public class MultipleTask extends AbstractMultipleTaskDataItemCollectorAndExecuterAndExecuter<Long>
{
    protected static transient Logger log = LoggerFactory.getLogger(MultipleTask.class);


    public Comparator<Long> getComparator()
    {
        return new Comparator<Long>()
        {
            public int compare(Long o1, Long o2)
            {
                return o1.compareTo(o2);
            }

            public boolean equals(Object obj)
            {
                return this == obj;
            }
        };
    }

    @Override
    public List<Long> getTaskDataItemList(String taskParameter, int fetchTaskDataItemNum, int taskItemNum, List<TaskItemStringAndParameterString> taskItemStringAndParameterStringList) throws Exception
    {
        System.out.println("======================================================================");
        List<Long> result = new ArrayList<Long>();
        int num = fetchTaskDataItemNum / taskItemStringAndParameterStringList.size();
        Random random = new Random(System.currentTimeMillis());
        String message = "获取数据...taskParameter=\"" + taskParameter + "\"]:";
        boolean isFirst = true;
        for (TaskItemStringAndParameterString s : taskItemStringAndParameterStringList)
        {
            long taskItem = Long.parseLong(s.getTaskItemString()) * 10000000L;
            for (int i = 0; i < num; i++)
            {
                result.add(taskItem + random.nextLong() % 100000L);
            }
            if (isFirst == false)
            {
                message = message + ",";
            } else
            {
                isFirst = false;
            }
            message = message + s.getTaskItemString() + "{" + s.getParameterString() + "}";
        }
        log.info(message);
        return result;
    }


    @Override
    public boolean dealWith(Long l) throws Exception
    {
        //System.out.println(l);
        return true;
    }
}